<?php

namespace common\models\user;

use Yii;

/**
 * This is the model class for table "{{%user_signin}}".
 *
 * @property int $id
 * @property int $user_id 用户id
 * @property string $date 签到日期
 * @property int $ipaddress 签到ip
 * @property int $continuation 连续签到天数
 * @property int $created_at 签到时间
 */
class UserSignin extends \common\base\ActiveRecord {

    use \common\traits\QueryInstanceActiveRecord;

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%user_signin}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['user_id', 'ipaddress', 'continuation', 'created_at'], 'integer'],
            [['date'], 'required'],
            [['date'], 'safe'],
            [['user_id', 'date'], 'unique', 'targetAttribute' => ['user_id', 'date']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '用户id',
            'date' => '签到日期',
            'ipaddress' => '签到ip',
            'continuation' => '连续签到天数',
            'created_at' => '签到时间',
        ];
    }

    public function getUser() {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    public function getIpaddress() {
        return long2ip($this->ipaddress);
    }

}
